//
//  MainViewController.m
//  stock
//
//  Created by Yangfan on 15/3/4.
//  Copyright (c) 2015年 4gread. All rights reserved.
//

#import "MainViewController.h"
#import "NSDate+Utilities.h"
#import "NSDate+CurrentTime.h"
#import "DrawRectView.h"
#import <AVOSCloud/AVOSCloud.h>

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewWillAppear:(BOOL)animated {
    [self updateData];
}

- (void)viewDidAppear:(BOOL)animated {
}

- (void)updateData {
    AVQuery *query = [AVQuery queryWithClassName:@"ZhangFu"];
    [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        NSDictionary *dic = [object objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"quotation"];// 0 上证   1 深证
        
        // 上证和深证指数
        NSString *str = [arr firstObject];
        NSArray *arrTest = [str componentsSeparatedByString:@","];
        if (shang_zheng == nil) {
            shang_zheng = [[NSMutableArray alloc] init];
        } else {
            [shang_zheng removeAllObjects];
        }
        [shang_zheng addObject:[arrTest objectAtIndex:2]];
        [shang_zheng addObject:[arrTest objectAtIndex:6]];
        str = [arr lastObject];
        arrTest = [str componentsSeparatedByString:@","];
        if (shen_zheng == nil) {
            shen_zheng = [[NSMutableArray alloc] init];
        } else {
            [shen_zheng removeAllObjects];
        }
        [shen_zheng addObject:[arrTest objectAtIndex:2]];
        [shen_zheng addObject:[arrTest objectAtIndex:6]];
        
        // 涨跌
        arr = [dic objectForKey:@"record"];// 0 上证   1 深证
        if(!cityButton.selected) {
            str = [arr firstObject];
        } else {
            str = [arr lastObject];
        }
        arrTest = [str componentsSeparatedByString:@","];
        rise = [[arrTest firstObject] floatValue];
        fall = [[arrTest lastObject] floatValue];

        // 开始更新页面数据
        NSString *flag = [NSString stringWithFormat:@"%.2f", [[shang_zheng firstObject] floatValue]];
        [shangzheng1Label setText:flag];
        UIFont *font = [UIFont systemFontOfSize:102 * scaleFrom6P];
        CGSize size = [self theWordSize:flag andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
        [shangzheng1Label setFrame:CGRectMake(730 * scaleFrom6P, 0, size.width, 530 * scaleFrom6P)];

        flag = [shang_zheng lastObject] ;
        [shangzheng2Label setText:flag];
        font = [UIFont systemFontOfSize:54 * scaleFrom6P];
        size = [self theWordSize:flag andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
        [shangzheng2Label setFrame:CGRectMake(730 * scaleFrom6P, 0, size.width, 722 * scaleFrom6P)];

        flag = [NSString stringWithFormat:@"%.2f", [[shen_zheng firstObject] floatValue]];
        [shenzheng1Label setText:flag];
        font = [UIFont systemFontOfSize:102 * scaleFrom6P];
        size = [self theWordSize:flag andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
        [shenzheng1Label setFrame:CGRectMake(730 * scaleFrom6P, 0, size.width, 1000 * scaleFrom6P)];

        flag = [shen_zheng lastObject] ;
        [shenzheng2Label setText:flag];
        font = [UIFont systemFontOfSize:54 * scaleFrom6P];
        size = [self theWordSize:flag andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
        [shenzheng2Label setFrame:CGRectMake(730 * scaleFrom6P, 0, size.width, 1188 * scaleFrom6P)];
        
        // 更新饼图
        [self updateBingTu];
        
        // 更新主力动态
        AVQuery *query = [AVQuery queryWithClassName:@"zhuli"];
        [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
            NSArray *zhuliArr = [object objectForKey:@"data"];
            float firstInt = [[zhuliArr objectAtIndex:12] floatValue] + [[zhuliArr objectAtIndex:14] floatValue];
            float secontInt = [[zhuliArr objectAtIndex:13] floatValue] + [[zhuliArr objectAtIndex:15] floatValue];
            if (firstInt < -secontInt) {
                [mairuLabel setText:@"流出"];
                bigOne = -secontInt;
            } else {
                bigOne = firstInt;
                [mairuLabel setText:@"流入"];
            }
            
            NSString *flag= [NSString stringWithFormat:@"%.1f亿", bigOne];
            UIFont *font = [UIFont systemFontOfSize:87 * scaleFrom6P];
            CGSize size = [self theWordSize:flag andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
            [duoshaoyiLabel setFrame:CGRectMake(844 * scaleFrom6P, 0, size.width, 80 * scaleFrom6P)];
            [duoshaoyiLabel setText:flag];


            [self updateTheUIColor];
            
        }];
    }];
}

- (void)updateTheUIColor {
    UIColor *color = [colorArr objectAtIndex:currentLevel];
    [self.view setBackgroundColor:color];
    navigationView.backgroundColor = color;

    [zhulidongtaiLabel setTextColor:color];
    [shangzhengzhishuLabel setTextColor:color];
    [shenzhengzhishuLabel setTextColor:color];

    [shangzheng1Label setTextColor:color];
    [shangzheng2Label setTextColor:color];
    [shenzheng1Label setTextColor:color];
    [shenzheng2Label setTextColor:color];
    [duoshaoyiLabel setTextColor:color];
    [mairuLabel setTextColor:color];
    /*
    // 商标的颜色淡一些
    float haha = 0.7;
    switch (currentLevel) {
        case 0:
            [renzhengLabel setTextColor:HexRGBAlpha(0xc0bdbd, 0.6)];
            break;
        case 1:
            [renzhengLabel setTextColor:HexRGB(0xc0bdbd)];
            break;
        case 2:
            [renzhengLabel setTextColor:HexRGBAlpha(0x7de795, haha)];
            break;
        case 3:
            [renzhengLabel setTextColor:HexRGBAlpha(0x5acd72, haha)];
            break;

        default:
            break;
    }
     */
    NSString *str = [NSString stringWithFormat:@"currentLevel%d", currentLevel + 1];
    [differentLevelImage setImage:[UIImage imageNamed:str]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    scaleFrom6P = Kwidth / 1242;

    [self setNavigationController];
    [self initParameter];
    [self appContent];
}

// 导航栏设置
- (void)setNavigationController {
    navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Kwidth, 171 * scaleFrom6P)];
    [self.view addSubview:navigationView];
    
    HumanTimeInfo *time = [NSDate currentTime];
    // 今天的日期 -- 前面的图标
    UIImageView *currentTimeImage = [[UIImageView alloc] init];
    UIImage *image = [UIImage imageNamed:@"currentTime"];
    [currentTimeImage setImage:image];
    [currentTimeImage setFrame:CGRectMake(0, 0, 121 * scaleFrom6P, 171 * scaleFrom6P)];
    [navigationView addSubview:currentTimeImage];
    
    // 今天的日期
    NSString *currentTime = [NSString stringWithFormat:@"%@.%@/%@", time.mounth, time.day, time.enWeakday];
    UILabel *timeLabel = [[UILabel alloc] init];
    UIFont *font = [UIFont systemFontOfSize:72 * scaleFrom6P];
    // label的大小
    CGSize size = [self theWordSize:currentTime andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [timeLabel setFrame:CGRectMake(144 * scaleFrom6P, 0, size.width, 171 * scaleFrom6P)];
    [timeLabel setFont:font];
    [timeLabel setText:currentTime];
    [timeLabel setTextColor:[UIColor whiteColor]];
    [navigationView addSubview:timeLabel];
    
    // 不同城市的切换按钮
    cityButton = [[UIButton alloc] init];
    [cityButton setFrame:CGRectMake(Kwidth - 413 * scaleFrom6P, 0, 413 * scaleFrom6P, 171 * scaleFrom6P)];
    [cityButton setBackgroundImage:[UIImage imageNamed:@"currentCityIsShangHai"] forState:UIControlStateNormal];
    [cityButton setBackgroundImage:[UIImage imageNamed:@"currentCityIsShenZheng"] forState:UIControlStateSelected];
    [cityButton addTarget:self action:@selector(changeCity:) forControlEvents:UIControlEventTouchUpInside];
    [navigationView addSubview:cityButton];
    
    // test By Adron
    UIButton *test  =  [[UIButton alloc] initWithFrame:CGRectMake(120, 0, 80, 57)];
    test.backgroundColor = [UIColor clearColor];
    [test addTarget:self action:@selector(testChangeColor:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:test];
}

- (void)testChangeColor:(UIButton *)sender {
    currentLevel ++;
    if (currentLevel == 4) {
        currentLevel = 0;
    }
    [self updateTheUIColor];
}

// 下面的功能区域
- (void)appContent {
    [self updateTopView];
    [self updateBingTu];
    [self updateBottomView];
}

// 对需要的变量等进行初始化
- (void)initParameter {
    mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 171 * scaleFrom6P, Kwidth, Kheight - 171 * scaleFrom6P)];
    [mainScrollView setShowsVerticalScrollIndicator:NO];
    [mainScrollView setShowsHorizontalScrollIndicator:NO];
    [mainScrollView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:mainScrollView];

    UIColor *color1 = HexRGB(0xfa3d46);
    UIColor *color2 = HexRGB(0xfa5e66);
    UIColor *color3 = HexRGB(0x7de795);
    UIColor *color4 = HexRGB(0x5acd72);
    colorArr = [[NSArray alloc] initWithObjects:color1, color2, color3, color4, nil];
    
    currentLevel = 0;
}

// 更新上面的界面在点击切换沪深的按钮
- (void)updateTopView {
    if (topView != nil) {
        for (UIView *view in [topView subviews]) {
            [view removeFromSuperview];
        }
    } else {
        topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Kwidth, Kheight - 171 * scaleFrom6P)];
        [topView setBackgroundColor:[UIColor clearColor]];
        [mainScrollView addSubview:topView];
    }
    
    
    
    // 今天的涨跌情况（偏涨，偏跌）
    UILabel *wordLabel = [[UILabel alloc] init];
    // label的大小
    UIFont *font = [UIFont systemFontOfSize:411 * scaleFrom6P];
    CGSize size = [self theWordSize:@"偏涨" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [wordLabel setFrame:CGRectMake((Kwidth - size.width)/2.0, 0, size.width, 963 * scaleFrom6P)];
    [wordLabel setFont:font];
    [wordLabel setText:@"偏涨"];
    [wordLabel setTextColor:[UIColor whiteColor]];
//    [wordLabel setBackgroundColor:[UIColor blueColor]];
//    [topView addSubview:wordLabel];
    
    // 不同等级下的笑容显示
    float imageMarginLeft;
    float imageMarginMiddle;
    float imageMarginBottom = 65;
    if(Kwidth < 330) {// 5以下设备
        imageMarginLeft = 55;
        imageMarginMiddle = 35;
        imageMarginBottom = 40;
    } else if(Kwidth > 330 && Kwidth < 400) {// 6设备
        imageMarginLeft = 65;
        imageMarginMiddle = 40;
    } else {// 6P
        imageMarginLeft = 74;
        imageMarginMiddle = 45;
    }

    // 左边笑脸的显示
    differentLevelImage = [[UIImageView alloc] init];
    UIImage *image = [UIImage imageNamed:@"currentLevel1"];
    float image_width = (Kwidth / 2.0) - imageMarginLeft - imageMarginMiddle;
    float image_height = image_width * image.size.height / image.size.width;
    [differentLevelImage setImage:image];
    [differentLevelImage setFrame:CGRectMake(imageMarginLeft, topView.frame.size.height - imageMarginBottom - image_height, image_width, image_height)];
    [topView addSubview:differentLevelImage];

    // 右边“整体打分”图片的显示
    UIImageView *dafen_word_image = [[UIImageView alloc] init];
    image = [UIImage imageNamed:@"zhengtidafen"];
    float dafen_word_image_width = (Kwidth / 2.0) - imageMarginLeft - imageMarginMiddle;
    float dafen_word_image_height = dafen_word_image_width * image.size.height / image.size.width;
    [dafen_word_image setImage:image];
    [dafen_word_image setFrame:CGRectMake(Kwidth / 2.0 + imageMarginMiddle, topView.frame.size.height - imageMarginBottom - dafen_word_image_height, dafen_word_image_width, dafen_word_image_height)];
    [topView addSubview:dafen_word_image];
 
    // 80
    UILabel *numberLabel = [[UILabel alloc] init];
    // label的大小
    float sizeNum = 1;
    size = CGSizeZero;
    while(size.width <= dafen_word_image_width) {
        font = [UIFont boldSystemFontOfSize:sizeNum];
        size = [self theWordSize:@"80" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
        sizeNum += 1;
    }
    font = [UIFont boldSystemFontOfSize:sizeNum - 1];
    size = [self theWordSize:@"80" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [numberLabel setFrame:CGRectMake(dafen_word_image.frame.origin.x, differentLevelImage.frame.origin.y, size.width, differentLevelImage.frame.size.height - dafen_word_image_height)];
    [numberLabel setFont:font];
    [numberLabel setText:@"80"];
    [numberLabel setTextAlignment:NSTextAlignmentCenter];
    [numberLabel setTextColor:[UIColor whiteColor]];
    [topView addSubview:numberLabel];

    // 偏涨 偏跌 文字图片
    UIImageView *mainImage = [[UIImageView alloc] init];
    image = [UIImage imageNamed:@"piandie"];
    float main_image_width = Kwidth - 80;
    float main_image_height = main_image_width * image.size.height / image.size.width;
    [mainImage setImage:image];
    [mainImage setFrame:CGRectMake(40, (differentLevelImage.frame.origin.y - main_image_height) / 2.0 - 20, main_image_width, main_image_height)];
    [topView addSubview:mainImage];
 
}

// 更新下面的界面在点击切换沪深的按钮--不包括饼图！！
- (void)updateBottomView {

    UIColor *color = [colorArr objectAtIndex:currentLevel];
    // 填补空白
    UIView *tianbu_view = [[UIView alloc] initWithFrame:CGRectMake(0, bingTuView.frame.origin.y + bingTuView.frame.size.height, Kwidth, 225 * scaleFrom6P)];
    tianbu_view.backgroundColor = [UIColor whiteColor];
    [mainScrollView addSubview:tianbu_view];

    if (bottomView != nil) {
        for (UIView *view in [bottomView subviews]) {
            [view removeFromSuperview];
        }
    } else {
        float bottomView_height;
            bottomView_height = [self scaleSizeFromiPhone6P:750];
        if (ISIPHONE4) {
        } else {
            bottomView_height = [self scaleSizeFromiPhone6P:943] + 30 * scaleFrom6P;
        }
        bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, tianbu_view.frame.origin.y + tianbu_view.frame.size.height, Kwidth, bottomView_height)];
        [bottomView setBackgroundColor:[UIColor whiteColor]];
        [mainScrollView addSubview:bottomView];
        [mainScrollView setContentSize:CGSizeMake(Kwidth, bottomView.frame.origin.y + bottomView.frame.size.height)];
    }
    
    // 主力动态
    zhulidongtaiLabel = [[UILabel alloc] init];
    // label的大小
    UIFont *font = [UIFont systemFontOfSize:66 * scaleFrom6P];
    CGSize size = [self theWordSize:@"主力动态" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [zhulidongtaiLabel setFrame:CGRectMake(214 * scaleFrom6P, 0, size.width, 95 * scaleFrom6P)];
    [zhulidongtaiLabel setFont:font];
    [zhulidongtaiLabel setText:@"主力动态"];
    [zhulidongtaiLabel setTextColor:color];
    [bottomView addSubview:zhulidongtaiLabel];

    // 上证指数
    shangzhengzhishuLabel = [[UILabel alloc] init];
    // label的大小
    font = [UIFont systemFontOfSize:66 * scaleFrom6P];
    size = [self theWordSize:@"上证指数" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [shangzhengzhishuLabel setFrame:CGRectMake(214 * scaleFrom6P, 223 * scaleFrom6P, size.width, 95 * scaleFrom6P)];
    [shangzhengzhishuLabel setFont:font];
    [shangzhengzhishuLabel setText:@"上证指数"];
    [shangzhengzhishuLabel setTextColor:color];
    [bottomView addSubview:shangzhengzhishuLabel];
    
    // 深证指数
    shenzhengzhishuLabel = [[UILabel alloc] init];
    // label的大小
    font = [UIFont systemFontOfSize:66 * scaleFrom6P];
    size = [self theWordSize:@"深证指数" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [shenzhengzhishuLabel setFrame:CGRectMake(214 * scaleFrom6P, 456 * scaleFrom6P, size.width, 95 * scaleFrom6P)];
    [shenzhengzhishuLabel setFont:font];
    [shenzhengzhishuLabel setText:@"深证指数"];
    [shenzhengzhishuLabel setTextColor:color];
    [bottomView addSubview:shenzhengzhishuLabel];
    
    if (!ISIPHONE4) {
        // @大盘天气预报
        renzhengLabel = [[UILabel alloc] init];
        // label的大小
        font = [UIFont systemFontOfSize:51 * scaleFrom6P];
        size = [self theWordSize:@"@大盘天气预报" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
        [renzhengLabel setFrame:CGRectMake((Kwidth - size.width) / 2.0, bottomView.frame.size.height - size.height - 10, size.width, size.height)];
        [renzhengLabel setFont:font];
        [renzhengLabel setText:@"@大盘天气预报"];
        [renzhengLabel setTextColor:color];
        [renzhengLabel setTextColor:HexRGBAlpha(0xc0bdbd, 0.6)];
        [bottomView addSubview:renzhengLabel];
    }
    // 买入
    mairuLabel = [[UILabel alloc] init];
    // label的大小
    font = [UIFont systemFontOfSize:51 * scaleFrom6P];
    size = [self theWordSize:@"买入" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [mairuLabel setFrame:CGRectMake(730 * scaleFrom6P, 0, size.width, 96 * scaleFrom6P)];
    [mairuLabel setFont:font];
    [mairuLabel setText:@"买入"];
    [mairuLabel setTextColor:color];
    [bottomView addSubview:mairuLabel];
 
    // 11.3亿
    duoshaoyiLabel = [[UILabel alloc] init];
    // label的大小
    font = [UIFont systemFontOfSize:87 * scaleFrom6P];
    size = [self theWordSize:@"11.3亿" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [duoshaoyiLabel setFrame:CGRectMake(844 * scaleFrom6P, 0, size.width, 80 * scaleFrom6P)];
    [duoshaoyiLabel setFont:font];
    [duoshaoyiLabel setText:@"11.3亿"];
    [duoshaoyiLabel setTextColor:color];
    [bottomView addSubview:duoshaoyiLabel];

    // 3506
    shangzheng1Label = [[UILabel alloc] init];
    // label的大小
    font = [UIFont systemFontOfSize:102 * scaleFrom6P];
    size = [self theWordSize:@"3506" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [shangzheng1Label setFrame:CGRectMake(730 * scaleFrom6P, 0, size.width, 530 * scaleFrom6P)];
    [shangzheng1Label setFont:font];
    [shangzheng1Label setText:@"3506"];
    [shangzheng1Label setTextColor:color];
    [bottomView addSubview:shangzheng1Label];
    
    // +0.23%
    shangzheng2Label = [[UILabel alloc] init];
    // label的大小
    font = [UIFont systemFontOfSize:54 * scaleFrom6P];
    size = [self theWordSize:@"+0.23%" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [shangzheng2Label setFrame:CGRectMake(730 * scaleFrom6P, 0, size.width, 722 * scaleFrom6P)];
    [shangzheng2Label setFont:font];
    [shangzheng2Label setText:@"+0.23%"];
    [shangzheng2Label setTextColor:color];
    [bottomView addSubview:shangzheng2Label];


    // 3601
    shenzheng1Label = [[UILabel alloc] init];
    // label的大小
    font = [UIFont systemFontOfSize:102 * scaleFrom6P];
    size = [self theWordSize:@"3601" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [shenzheng1Label setFrame:CGRectMake(730 * scaleFrom6P, 0, size.width, 1000 * scaleFrom6P)];
    [shenzheng1Label setFont:font];
    [shenzheng1Label setText:@"3601"];
    [shenzheng1Label setTextColor:color];
    [bottomView addSubview:shenzheng1Label];

    // +0.28%
    shenzheng2Label = [[UILabel alloc] init];
    // label的大小
    font = [UIFont systemFontOfSize:54 * scaleFrom6P];
    size = [self theWordSize:@"+0.28%" andFont:font andCGSize:CGSizeMake(Kwidth, Kheight)];
    [shenzheng2Label setFrame:CGRectMake(730 * scaleFrom6P, 0, size.width, 1188 * scaleFrom6P)];
    [shenzheng2Label setFont:font];
    [shenzheng2Label setText:@"+0.28%"];
    [shenzheng2Label setTextColor:color];
    [bottomView addSubview:shenzheng2Label];
}

//饼图的更新
- (void)updateBingTu {
    if (bingTuView != nil) {
        for (UIView *view in [bingTuView subviews]) {
            [view removeFromSuperview];
        }
    } else {
        bingTuView = [[UIView alloc] initWithFrame:CGRectMake(0, topView.frame.size.height, Kwidth, 840 * scaleFrom6P)];
        [mainScrollView addSubview:bingTuView];
    }

    DrawRectView *drawRectView = [[DrawRectView alloc] initWithFrame:bingTuView.bounds];
    NSArray *arr = [[NSArray alloc] initWithObjects:@(rise),@(fall),@508,@345, nil];
    [drawRectView setDataArr:arr];
    [bingTuView addSubview:drawRectView];
}

- (void)changeCity:(UIButton *)sender {
    [sender setSelected:!sender.selected];
    [self updateData];
}



@end
